/* * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *  @file:     mainwindow.cpp
 *  @brief:    主窗口类
 *  @author:   caoshi
 *  @time:     2021-08-20 21:26:00
 * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include "setbgcolordialog.h"
#include <QPainter>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    setWindowTitle("自定义对话框");
    connect(this->ui->changeBgBtn, &QPushButton::clicked, [=]() {
       // qDebug() << "按钮被按下了";
       this->setBgDialog.show();
    });


    connect(&setBgDialog, &SetBgColorDialog::okClicked, this, &MainWindow::setBgColor);

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::setBgColor(QString color)
{
    qDebug() << "父组件中获取到 color = " + color;
    qDebug() << qHash(color);
    this->bg = qHash(color);

}

/**
 *  获取画刷
 * @brief MainWindow::getBrush
 * @return
 */
QBrush MainWindow::getBrush()
{
    QBrush brush;
    switch (this->bg) {
    case 112785:
        brush = QBrush(Qt::red);
        break;
    case 98619139:
        brush = QBrush(Qt::green);
        break;
    case 3560727668:
        brush = QBrush(Qt::yellow);
        break;
    case 3027034:
        brush = QBrush(Qt::blue);
        break;
    }
    return brush;
}

void MainWindow::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);
    QPainter painter(this);
    painter.setPen(Qt::NoPen);
    QBrush brush = this->getBrush();
    painter.setBrush(brush);
    painter.drawRect(this->rect());
}
